(function loadCalendar($){
    var eCalendar = function(options,object){
        var adDay = new Date().getDate();
        var adMonth = new Date().getMonth();
        var adYear = new Date().getFullYear();
        var dDay = adDay;
        var dMonth = adMonth;
        var dYear = adYear;
        var instance = object;
        var setting = $.extend({}, $.fn.eCalendar.defaults,options);
        setting.firstDayOfWeek = 0;

        var previousMonth = function(){
            if(dMonth>0){
                dMonth--;
            }else{
                dMonth=11;
                dYear--;
            }
            print();
        }
        var nextMonth = function(){
            if (dMonth < 11) {
                dMonth++;
            } else {
                dMonth = 0;
                dYear++;
            }
            print();
        }

        function print(){
            var dWeekDayOfMonthStart = new Date(dYear,dMonth,1).getDay()-setting.firstDayOfWeek;
            if(dWeekDayOfMonthStart<0){
                dWeekDayOfMonthStart = 6-((dWeekDayOfMonthStart+1)* -1);
            }
            var dLastDayOfMonth = new Date(dYear,dMonth+1,0).getDate();
            var dLastDayOfPreviousMonth = new Date(dYear,dMonth+1,0).getDate()-dWeekDayOfMonthStart+1;
            $(".c-month").html(setting.months[dMonth]+' '+dYear);

            var day = 1;
            var dayOfNextMonth = 1;
            var $tbody = $(".clander tbody");
            var $tr = $("<tr>");
            var $td = $("<td>");
            var td = "";
            var tr = "";
            for(var i=0;i<42;i++){
                if(i<dWeekDayOfMonthStart){
                    if(i%7===0){
                        $tr = $("<tr></tr>");
                        $td = $("<td class='"+(i+1)+"'><span>"+(dLastDayOfPreviousMonth++)+"</span></td>");
                        $tr.append($td);
                        $tbody.append($tr);
                    }else{
                        $td = $("<td class='"+(i+1)+"'><span>"+(dLastDayOfPreviousMonth++)+"</span></td>");
                        $tr.append($td);
                    }
                    $td.addClass("nodata");
                }else if(day<=dLastDayOfMonth){
                    //var ymd = dYear+
                    var tdClass = "date_active"+dYear+"-"+(dMonth+1)+"-"+day;
                    if(i%7===0){
                        $tr = $("<tr></tr>");
                        $td = $("<td date-active='"+tdClass+"'><span>"+(day)+"</span></td>");
                        $tr.append($td);
                        $tbody.append($tr);
                    }else{
                        $td = $("<td date-active='"+tdClass+"'><span>"+(day)+"</span></td>");
                        $tr.append($td);
                    }

                    if(day==dDay && adMonth == dMonth &&adYear ==dYear){
                        $td.addClass("today");
                    }

                    _.each(setting.eventsInfo,function(event){
                        var activeDate = new Date(event.activeStartTime);

                        if(activeDate.getDate()==day && activeDate.getMonth() == dMonth && activeDate.getFullYear()==dYear){
                            $td.addClass("current");
                            $(".clander").on("click","td",function(){
                                console.log(111);
                            })

                        }
                    })
                    day++;
                }else{
                    if(i%7===0){
                        $tr = $("<tr></tr>");
                        $td = $("<td class='"+(i+1)+"'><span>"+(dayOfNextMonth++)+"</span></td>");
                        $tr.append($td);
                        $tbody.append($tr);
                    }else{
                        $td = $("<td class='"+(i+1)+"'><span>"+(dayOfNextMonth++)+"</span></td>");
                        $tr.append($td);
                    }
                    $td.addClass("nodata");
                }
            }
        }
        $(".c_left").off("click").on("click",function(){
            var selectedDate = $(".selectedTd").attr("date-active");
            if( $(".clander_same_day").parent().find("."+selectedDate).length>0){
                $(".clander_same_day").parent().find("."+selectedDate).parent().siblings().detach();
            }

            $(".clander tbody").html("");
            previousMonth();
            var categoryId = "5";
            var queryObj = {};
            var signUpInfo = {};
            queryObj.safeTypeId = common.getParameter("safeTypeId") || 0;
            if(queryObj.safeTypeId){
                common.setActiveItem(queryObj.safeTypeId);
            }
            queryObj.year = dYear;
            queryObj.month = dMonth+1;
            queryActivity($(""),queryObj);
        })
        $(".c_right").off("click").on("click",function(){
            $(".clander tbody").html("");
            nextMonth();
            var categoryId = "5";
            var queryObj = {};
            var signUpInfo = {};
            queryObj.safeTypeId = common.getParameter("safeTypeId") || 0;
            if(queryObj.safeTypeId){
                common.setActiveItem(queryObj.safeTypeId);
            }
            queryObj.year = dYear;
            queryObj.month = dMonth+1;
            queryActivity($(""),queryObj);
        })
        return print();
    }
    $.fn.extend({
        eCalendar:function(oInit) {
            return this.each(function () {
                return eCalendar(oInit, $(this));
            })
        }
    })
    //$.fn.eCalendar = function(oInit){
    //    return this.each(function(){
    //        return eCalendar(oInit,$(this));
    //    })
    //}

    $.fn.eCalendar.defaults = {
        months:['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.'],
        events:["2016-11-2","2016-11-4","2016-10-12"],
        eventsInfo:[]
    }
}(jQuery))